package cn.tedu.sgs.user.service.impl;

import cn.tedu.sgs.base.exception.ServiceException;
import cn.tedu.sgs.base.response.StatusCode;
import cn.tedu.sgs.user.mapper.UserMapper;
import cn.tedu.sgs.user.pojo.dto.*;
import cn.tedu.sgs.user.pojo.entity.User;
import cn.tedu.sgs.user.pojo.vo.UserVO;
import cn.tedu.sgs.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class UserServiceImpl implements UserService {
	@Autowired
	UserMapper userMapper;

	@Override
	public UserVO login(UserLoginParam userLoginParam) {
		log.debug("用户登录业务,参数:{}" + userLoginParam);
		UserVO userVO = userMapper.selectByUsername(userLoginParam.getUsername());
		if (userVO == null) {
			throw new ServiceException(StatusCode.PASSWORD_ERROR);
		}
		if (!userVO.getPassword().equals(userLoginParam.getPassword())) {
			throw new ServiceException(StatusCode.PASSWORD_ERROR);
		}
		// 权限验证,如果用户已有身份是user但选择了admin身份，则返回权限不足
		if ("admin".equals(userLoginParam.getRole()) && !"admin".equals(userVO.getRole())) {
			throw new ServiceException(StatusCode.ACCESS_DENIED);
		}
		// 权限验证,如果用户已有身份是admin但选择了user身份，足则返回权限有误
		if ("admin".equals(userVO.getRole()) && "user".equals(userLoginParam.getRole())) {
			throw new ServiceException(StatusCode.ACCESS_ERROR);
		}
		log.debug("用户登录业务,查询结果:{}",userVO);
		return userVO;

	}

	@Override
	public void reg(UserRegParam userRegParam) {
		//检查用户是否存在
		UserVO userVO = userMapper.selectByUsername(userRegParam.getUsername());
		if (userVO != null) {
			throw new ServiceException(StatusCode.USERNAME_ALREADY_EXISTS);
		}
		userRegParam.setPoints(0);
		userMapper.insertUser(userRegParam);
		log.debug("用户注册业务,参数:{}",userRegParam);
	}

	@Override
	public List<UserVO> selectUser(UserQuery userQuery) {
		log.debug("查询用户业务,参数:{}",userQuery);
		List<UserVO> list = userMapper.selectUser(userQuery);
		return list;
	}

	@Override
	public void changePassword(UserChangePasswordParam userChangePasswordParam) {
		log.debug("修改密码业务参数:{}",userChangePasswordParam);
		User user = new User();
		BeanUtils.copyProperties(userChangePasswordParam,user);
		user.setPassword(userChangePasswordParam.getPassword());
		userMapper.update(user);
	}

	@Override
	public void saveUser(UserSaveParam userSaveParam) {
		log.debug("保存用户业务参数:{}",userSaveParam);
		User user = new User();
		BeanUtils.copyProperties(userSaveParam,user);
		if (user.getId() == null) {
			user.setPassword("123456");
			user.setPoints(0);
			userMapper.insert(user);
		}else {
			userMapper.update(user);
		}
	}

	@Override
	public void resetPassword(Long userId) {
		log.debug("重置密码业务参数:{}",userId);
		User user = new User();
		user.setId(userId);
		user.setPassword("000000");
		userMapper.update(user);
	}

	@Override
	public void deleteUser(Long userId) {
		log.debug("删除用户业务参数:{}",userId);
		userMapper.deleteById(userId);
	}

	@Override
	public void savePoints(UserUpdateParam userUpdateParam) {
		log.debug("修改积分业务参数:{}",userUpdateParam);
		User user = new User();
		BeanUtils.copyProperties(userUpdateParam,user);
		userMapper.update(user);
	}
}
